import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2020/10/19 15:08
 */
public class Test03 {
    public static void main(String[] args) throws IOException {
        //创建两个对象，然后放到集合中
        List<User> users = new ArrayList<>();
        users.add(new User(0,"liliya",18));
        users.add(new User(1,"luosaliya",16));

        File file = new File("user.obj");
        if (!file.exists()) file.createNewFile();
        try(
              ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
              ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
                ){

            //序列化对象
            objectOutputStream.writeObject(users);
            //反序列化对象
            List<User> userList = (List<User>) inputStream.readObject();
            for (User user : userList) {
                System.out.println(user);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }
}
